package serviceImp;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import pojo.Channel;
import pojo.Movie;
import pojo.Tvshow;
import pojo.Video;
import dao.ChannelHome;
import dao.TvshowHome;
import dao.VideoHome;
import service.ITvShowService;

public class TvShowServiceImp implements ITvShowService {

    
    private static final Logger _logger = Logger.getLogger(TvShowServiceImp.class);
    
    private TvshowHome tvShowDAO;
    
    private ChannelHome channelDAO;
    
    private VideoHome videoDAO;
    
    public TvShowServiceImp() {
	super();
    }
    

    public void setTvShowDAO(TvshowHome tvShowDAO) {
        this.tvShowDAO = tvShowDAO;
    }




    public void setChannelDAO(ChannelHome channelDAO) {
        this.channelDAO = channelDAO;
    }




    public void setVideoDAO(VideoHome videoDAO) {
        this.videoDAO = videoDAO;
    }




    public Tvshow getTvshowbyId(int tvID) {
	Tvshow tvshow = tvShowDAO.findById(tvID);
	
	if (tvshow == null) {
	    _logger.log(Level.ERROR, String.format("Tv %d not found", tvID));
	}
	return tvshow;
    }

    public List<Tvshow> getAllTvshows() {
	List<Tvshow> tvshows = tvShowDAO.findByExample(new Tvshow());
	
	_logger.log(Level.INFO,String.format("%d tvshow found", tvshows.size()));
	
	return tvshows;
	
    }



    public List<Tvshow> searchTvshows(String keyword) {
	
	List<Tvshow> tvshows = tvShowDAO.findByKeyword(keyword) ;
	
	_logger.log(Level.INFO,String.format("%d tvshow found", tvshows.size()));
	
	return tvshows;
    }

    public boolean addTvshow(Tvshow tvshow) {
	Tvshow newTvshow = tvshow;
	
	try {
	    tvShowDAO.attachDirty(newTvshow);
	    return true;
	} catch (Exception e) {
	    _logger.log(Level.ERROR, e);
	    
	    return false;
	}

    }

    public boolean removeTvshow(Tvshow tvshow) {
	try {
	    tvShowDAO.delete(tvshow);
	    return true;
	} catch (Exception e) {
	    _logger.log(Level.ERROR, e);
	    
	    return false;
	}
    }

    public boolean hasVideo(int tvID) {
	Video video = new Video();
	
	video.setVTvshow(tvID);
	
	List<Video> videos = videoDAO.findByExample(video);
	
	_logger.log(Level.INFO, String.format("%d video found for tv %d", videos.size(), tvID));
	
	if (videos.size() >0) {
	    return true;
	}
	
	return false;
    }

    public List<Tvshow> getAllTvshowsByChannel(int channel_id) {
	
	Channel channel = channelDAO.findById(channel_id);
	
	if (channel == null) {
	    _logger.log(Level.ERROR, "Channel not existed");
	    return null;
	}
	List<Tvshow> tvshows = tvShowDAO.findByChannel(channel.getChannelId());
	
	_logger.log(Level.INFO,String.format("%d tvshow found", tvshows.size()));
	
	return tvshows;
    }


    public void deletTvshow(int tvid) {
	try {
	    Tvshow tvshow = getTvshowbyId(tvid);
	    tvShowDAO.delete(tvshow);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return;
	
    }


    public void updateTvshow(Tvshow tvshow) {
	try {
	    tvShowDAO.attachDirty(tvshow);

	} catch (Exception e) {
	    e.printStackTrace();
	}
	return;
	
    }

}
